﻿using Shop.Core.Consts;
using Shop.Core.Repository;
using Shop.Core.Share;
using Shop.IServices;
using Shop.Model.Dto;
using Shop.Model.Dto.Enc;
using Shop.Model.Entity.Enc;
using Shop.Model.View.Enc;

namespace Shop.Services;

public class EncyclopediaAppService: IEncyclopediaAppService
{
    private readonly RepositoryBase<EncyclopediaGroup> _groupEfRepository;
    private readonly RepositoryBase<EncyclopediaContent> _contentEfRepository;

    public EncyclopediaAppService(RepositoryBase<EncyclopediaGroup> groupEfRepository
        , RepositoryBase<EncyclopediaContent> contentEfRepository)
    {
        _groupEfRepository = groupEfRepository;
        _contentEfRepository = contentEfRepository;
    }
    
    /// <summary>
    /// 获取分组
    /// </summary>
    /// <param name="language"></param>
    /// <returns></returns>
    public async Task<List<EncyclopediaGroupVo>> GetGroupList(string language)
    {
        var iszh = language.Equals(LanguageConsts.ZH);
        return await _groupEfRepository.Where(x => x.IsDel == false)
            .OrderBy(x => x.Sort)
            .Select(x => new EncyclopediaGroupVo
            {
                Id = x.Id,
                Name = iszh ? x.Name : x.UighurName,
                Describe = iszh ? x.Describe : x.UighurDescribe,
                Basemap = x.Background
            }).ToListAsync();
    }
    
    /// <summary>
    /// 获取问题
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PageModel<EncyclopediaContentVo>> GetContentList(GetContentListDto input)
    {
        var iszh = input.Language.Equals(LanguageConsts.ZH);;
        var count = await _contentEfRepository.CountAsync(x => x.GroupId == input.GroupId && x.IsDelete == false);
        var data = await _contentEfRepository.Where(x => x.GroupId == input.GroupId && x.IsDelete == false)
            .Skip(input.PageSize * (input.PageIndex - 1))
            .Take(input.PageSize)
            .Select(x => new EncyclopediaContentVo {
                Id = x.Id,
                Question = iszh ? x.Question : x.UighurQuestion,
                Answer  = iszh ? x.Answer : x.UighurAnswer,
            })
            .ToListAsync();
        return new PageModel<EncyclopediaContentVo>(input.PageIndex,input.PageSize,data,count);
    }
}